/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-09-03 01:20:22
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-09-29 09:07:29
 * @FilePath: \keep_motion_api\controllers\sport\trainingPlans.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const {TrainingPlan} = require('@/models/sport');
const { clearObject } = require('@/utils');

const getTrainingPlans = async (req, res) => {
  try {
    const { name, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const trainingPlans = await TrainingPlan.getTrainingPlans(name, offset, limit);
    res.status(200).send(trainingPlans);
  } catch (error) {
    res.status(500).json({ message: '获取训练计划失败', error: error.message });
  }
}
const createTrainingPlans = async (req, res) => {
  try {
    const body = req.body;
    const params = clearObject(body);
    const trainingPlan = await TrainingPlan.createTrainingPlans(params);
    res.status(200).send(trainingPlan);
  } catch (error) {
    res.status(500).json({ message: '创建训练计划失败', error: error.message });
  }
}
const updateTrainingPlans = async (req, res) => {
  try {
    const { id } = req.params;
    const body = req.body;
    const params = clearObject(body);
    const trainingPlan = await TrainingPlan.updateTrainingPlans(id, params);
    res.status(200).send(trainingPlan);
  } catch (error) {
    res.status(500).json({ message: '更新训练计划失败', error: error.message });
  }
}
const recommendTrainingPlans = async (req, res) => {
  try {
    const { id } = req.params;
    const { is_recommended } = req.body;
    const trainingPlan = await TrainingPlan.recommendTrainingPlan(id, is_recommended);
    res.status(200).send(trainingPlan);
  } catch (error) {
    res.status(500).json({ message: '更新训练计划推荐状态失败', error: error.message });
  }
}

module.exports = {
  getTrainingPlans,
  createTrainingPlans,
  updateTrainingPlans,
  recommendTrainingPlans
}
